package com.nfc.lock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nfc.lock.common.result.Result;
import com.nfc.lock.dto.request.LockOperateRequest;
import com.nfc.lock.dto.response.LockListVO;
import com.nfc.lock.service.INfcLockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 锁具Controller
 */
@Tag(name = "锁具管理")
@RestController
@RequestMapping("/api/v1/lock")
@RequiredArgsConstructor
public class NfcLockController {

    private final INfcLockService lockService;

    @Operation(summary = "获取锁具列表")
    @GetMapping("/list")
    public Result<IPage<LockListVO>> getLockList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "20") Integer pageSize,
            @Parameter(description = "NFC ID") @RequestParam(required = false) String lockNfcId,
            @Parameter(description = "配电室ID") @RequestParam(required = false) Long roomId,
            @Parameter(description = "区域ID") @RequestParam(required = false) Long regionId,
            @Parameter(description = "锁具状态") @RequestParam(required = false) Integer lockStatus
    ) {
        return Result.success(lockService.getLockList(pageNum, pageSize, lockNfcId, roomId, regionId, lockStatus));
    }

    @Operation(summary = "新增锁具")
    @PostMapping("/add")
    public Result<Void> addLock(
            @Parameter(description = "NFC ID") @RequestParam String lockNfcId,
            @Parameter(description = "配电室ID") @RequestParam Long roomId
    ) {
        lockService.addLock(lockNfcId, roomId);
        return Result.success();
    }

    @Operation(summary = "开锁")
    @PostMapping({"/open", "/nfc/open"})
    public Result<Void> openLock(@Valid @RequestBody LockOperateRequest request) {
        lockService.openLock(request);
        return Result.success();
    }

    @Operation(summary = "关锁")
    @PostMapping({"/close", "/nfc/close"})
    public Result<Void> closeLock(@Valid @RequestBody LockOperateRequest request) {
        lockService.closeLock(request);
        return Result.success();
    }

    @Operation(summary = "查询锁具详情")
    @GetMapping("/detail/{lockId}")
    public Result<LockListVO> getLockDetail(@Parameter(description = "锁具ID") @PathVariable Long lockId) {
        return Result.success(lockService.getLockDetail(lockId));
    }

    @Operation(summary = "报修锁具")
    @PostMapping("/reportDamage")
    public Result<Void> reportDamage(
            @Parameter(description = "锁具ID") @RequestParam Long lockId,
            @Parameter(description = "报修说明") @RequestParam String remark
    ) {
        lockService.reportDamage(lockId, remark);
        return Result.success();
    }

    @Operation(summary = "删除锁具")
    @DeleteMapping("/delete/{lockId}")
    public Result<Void> deleteLock(@Parameter(description = "锁具ID") @PathVariable Long lockId) {
        lockService.deleteLock(lockId);
        return Result.success();
    }
}
